package com.siss.tenancy.elastic.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;


/**
 * 类名: DynamicRedisTemplate.java
 * 描述: 切换Redis template根据 contextLocal's dbIndex
 * 创建人: yingw
 * 创建时间: 2018年10月10日上午10:52:02
 */

public class DynamicRedisTemplate<K, V> extends AbstractRoutingRedisTemplate<K, V> { 
	private final static Logger logger = LoggerFactory.getLogger(DynamicRedisTemplate.class);
	
    private int database;

    private String hostName;
    
    private int port;
    
    private String password;
    
    private int timeout;
    
    private int maxIdle;
    
    private int minIdle;
        
    private int maxTotal;
    
    private long maxWaitMillis;
    
    private boolean testOnBorrow;
    
    protected DynamicRedisTemplate(int database, String hostName, int port, String password, int timeout, int maxIdle, int minIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow){
    	this.database = database;
    	this.hostName = hostName;
    	this.port = port;
    	this.password = password;
    	this.timeout = timeout;
    	this.maxIdle = maxIdle;
    	this.minIdle = minIdle;
    	this.maxTotal = maxTotal;
    	this.maxWaitMillis = maxWaitMillis;
    	this.testOnBorrow = testOnBorrow;
    }
    
	/*public static void setCurrentDbIndex(Integer dbIndex){
		TenantContextHolder.setRedisIndex(dbIndex);
	}
	
	public static Integer getCurrentDbIndex(){
		return TenantContextHolder.getRedisIndex();
	}
	
	public static void remove(){
		TenantContextHolder.removeRedis();
	}*/
	
	@Override
	protected Integer determineCurrentLookupKey() {
		/*Integer dbIndex;
		if(DynamicRedisTemplate.getCurrentDbIndex()==null){
			dbIndex =  0;
		}else{
			dbIndex = DynamicRedisTemplate.getCurrentDbIndex();
		}
		return dbIndex;*/
		return database;
	}
	
	@Override
	protected RedisTemplate createRedisTemplateOnMissing(Integer lookupKey) {
		StringRedisTemplate template = new StringRedisTemplate();
		template.setConnectionFactory(this.createConnectionFactory(lookupKey));
		 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
	     ObjectMapper om = new ObjectMapper();
	     om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
	     jackson2JsonRedisSerializer.setObjectMapper(om);
	     template.setValueSerializer(jackson2JsonRedisSerializer);
	     template.afterPropertiesSet();
		 logger.debug("【redis路由器】create template for database:"+lookupKey);
		return template;
	}
	
    
    private JedisConnectionFactory createConnectionFactory(Integer db){
    		JedisConnectionFactory redisConnectionFactory  = new JedisConnectionFactory();
    	    redisConnectionFactory.setDatabase(db);
    	    redisConnectionFactory.setHostName(hostName);
    	    redisConnectionFactory.setPort(port);
    	    redisConnectionFactory.setPassword(password);
    	    redisConnectionFactory.setTimeout(timeout);
        	
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 
            jedisPoolConfig.setMaxIdle(maxIdle); 
            jedisPoolConfig.setMinIdle(minIdle);
            jedisPoolConfig.setMaxTotal(maxTotal); 
            jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);  
            jedisPoolConfig.setTestOnBorrow(testOnBorrow); 
            redisConnectionFactory.setPoolConfig(jedisPoolConfig);
            redisConnectionFactory.afterPropertiesSet();
            return redisConnectionFactory;
    }
	
}
